package com.ABdolphin.forum.services.impl;

import com.ABdolphin.forum.common.AppResult;
import com.ABdolphin.forum.common.ResultCode;
import com.ABdolphin.forum.dao.MessageMapper;
import com.ABdolphin.forum.exception.ApplicationException;
import com.ABdolphin.forum.model.Message;
import com.ABdolphin.forum.model.User;
import com.ABdolphin.forum.services.IMessageService;
import com.ABdolphin.forum.services.IUserService;
import com.ABdolphin.forum.utils.StringUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MessageServiceImpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;

    @Resource
    private IUserService userService;


    @Override
    public void create(Message message) {
        if (message == null || message.getPostUserId() == null ||
                message.getReceiveUserId() == null ||
                StringUtil.isEmpty(message.getContent())){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message InsertMessage = new Message();
        InsertMessage.setPostUserId(message.getPostUserId());
        InsertMessage.setReceiveUserId(message.getReceiveUserId());
        InsertMessage.setContent(message.getContent());
        InsertMessage.setUpdateTime(new Date());
        InsertMessage.setCreateTime(new Date());
        InsertMessage.setState((byte)0);
        InsertMessage.setDeleteState((byte)0);
        int result = messageMapper.insertSelective(InsertMessage);
        if (result<=0){
            log.warn(ResultCode.FAILED.toString() + ", 受影响行数不等于1,");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
    }

    @Override
    public Integer getUnReadCount(Long receiveUserId) {
        if (receiveUserId==null||receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User receiveUser = userService.selectById(receiveUserId);
        if (receiveUser==null){
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        return messageMapper.selectUnreadCount(receiveUserId);
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if (receiveUserId==null||receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User receiveUser = userService.selectById(receiveUserId);
        if (receiveUser==null){
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        return messageMapper.selectByReceiveUserId(receiveUserId);
    }

    @Override
    public Message selectById(Long id) {
        if (id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return messageMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateStateById(Long id, byte state) {
        if (id==null||id<=0||
                state>2||state<0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = selectById(id);
        if (message==null){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        Message updateMessage = new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        updateMessage.setUpdateTime(new Date());
        messageMapper.updateByPrimaryKeySelective(updateMessage);
    }

    @Override
    public void reply(Long repliedId, String content) {
        if (repliedId==null||repliedId<=0||StringUtil.isEmpty(content)){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //站内信是否存在/检验状态
        Message message = selectById(repliedId);
        if (message==null||message.getState()!=1){
            log.warn("站内信状态存在问题,站内信id= "+message.getId()+", 原状态为= "+message.getState());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
        //构建新站内信,并发送
        Message insertMessage = new Message();
        insertMessage.setPostUserId(message.getReceiveUserId());
        insertMessage.setReceiveUserId(message.getPostUserId());
        insertMessage.setContent(content);
        create(insertMessage);
        //修改状态
        updateStateById(message.getId(), (byte) 2);
    }
}
